package com.xcenter.cabinet.folder.controller;

import java.io.File;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xcenter.cabinet.folder.vo.FolderVO;

@RestController
@RequestMapping("cabinet/folder")
public class FolderController {
	
	@Value("${cabinet.root}")
	private String root;

	@RequestMapping
	public FolderVO list() {
		File file = new File(root);
		FolderVO vo = new FolderVO(root, root);
		File[] listFiles = file.listFiles();
		if (listFiles.length > 0) {
			for (File file2 : listFiles) {
				recBuildFolderVO(file2, vo, root.length() + 1);
			}
		}
		return vo;
	}
	
	/**
	 * 递归构建目录
	 * @param file
	 * @param vo
	 */
	private void recBuildFolderVO(File file, FolderVO vo, int rootLen) {
		if (file.isDirectory()) {
			FolderVO folderVO = new FolderVO(file.getName(), file.getPath().substring(rootLen));
			vo.addNode(folderVO);
			File[] listFiles = file.listFiles();
			if (listFiles.length > 0) {
				for (File file2 : listFiles) {
					recBuildFolderVO(file2, folderVO, rootLen);
				}
			}
		}
	}
	
}
